package com.example.serialportdemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.serialportdemo.R;

/**
 * @auth: njb
 * @date: 2023/12/31 12:34
 * @desc: 描述
 */
public class AirConditionerView extends LinearLayout {

    public AirConditionerDisplayView getDisplayView() {
        return displayView;
    }

    private AirConditionerDisplayView displayView;


    private Paint textTemperaturePaint;
    private Paint textUnitPaint;
    private int textTemperatureColor = Color.WHITE;
    private int textUnitColor = Color.WHITE;
    private float textTemperatureSize = 26;
    private float textUnitSize = 26;


    private Paint textDescribePaint;
    private int textDescribeColor = Color.GREEN;
    private float textDescribeSize = 26;

    private float width = 0;
    private float height = 0;
    private float minLen = 0;

    private String describe = "室内温度";
    private String temperatureStr;
    private float temperatureInt;

    private float x, y;//圆心坐标
    private float describePanddinBottom = 0;
    private float describePanddinTop = 0;//describePanddinTop 优先级最高 其次是describePanddinBottom
    private float textUnitPanddinBottom = 0;
    private float textUnitPanddinTop = 0;//textUnitPanddinTop 优先级最高 textUnitPanddinBottom
    private float textTemperatureBottom = 0;
    private float textTemperatureTop = 0;//textTemperatureTop 优先级最高 textTemperatureBottom

    private float maxTemperature;
    private float minTemperature;
    private int mDensity;

    public AirConditionerView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public AirConditionerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public AirConditionerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }


    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        displayView = new AirConditionerDisplayView(context, attrs, defStyleAttr);
        ViewGroup.LayoutParams layoutParams = displayView.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            displayView.setBackgroundColor(Color.TRANSPARENT);
            displayView.setLayoutParams(layoutParams);
        } else {
            layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            displayView.setBackgroundColor(Color.TRANSPARENT);
            displayView.setLayoutParams(layoutParams);
        }
        addView(displayView);
        if (displayView != null) {
//            displayView.setOnTemperatureChangeListener(this);
//            temperatureInt = displayView.getNowTemperature();
            temperatureStr = temperatureInt + "";
        }

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AirConditionerView);
            if (typedArray != null) {
                textTemperatureSize = typedArray.getDimension(R.styleable.AirConditionerView_textTemperatureSize, 30);
                textDescribeSize = typedArray.getDimension(R.styleable.AirConditionerView_textDescribeSize, 30);
                describePanddinBottom = typedArray.getDimension(R.styleable.AirConditionerView_describePanddinBottom, 0);
                describePanddinTop = typedArray.getDimension(R.styleable.AirConditionerView_describePanddinTop, 0);
                textUnitPanddinBottom = typedArray.getDimension(R.styleable.AirConditionerView_textUnitPanddinBottom, 0);
                textUnitPanddinTop = typedArray.getDimension(R.styleable.AirConditionerView_textUnitPanddinTop, 0);
                textTemperatureBottom = typedArray.getDimension(R.styleable.AirConditionerView_textTemperatureBottom, 0);
                textTemperatureTop = typedArray.getDimension(R.styleable.AirConditionerView_textTemperatureTop, 0);
//                describePanddinTop = typedArray.getDimension(R.styleable.AirConditionerView_describePanddinTop, 0);
                textUnitSize = typedArray.getDimension(R.styleable.AirConditionerView_textUnitSize, 26);
                textTemperatureColor = typedArray.getInteger(R.styleable.AirConditionerView_textTemperatureColor, Color.GREEN);
                textDescribeColor = typedArray.getInteger(R.styleable.AirConditionerView_textDescribeColor, Color.parseColor("#ecf6f3"));
                textUnitColor = typedArray.getInteger(R.styleable.AirConditionerView_textUnitColor, Color.parseColor("#ecf6f3"));
                describe = typedArray.getString(R.styleable.AirConditionerView_describe);
                if (describe == null) {
                    describe = "室内温度";
                }
                maxTemperature = typedArray.getFloat(R.styleable.AirConditionerView_maxTemperature, 20);
                displayView.setMaxTemp(maxTemperature);
                minTemperature = typedArray.getFloat(R.styleable.AirConditionerView_minTemperature, 10);
                displayView.setMinTemp(minTemperature);
                mDensity = typedArray.getInt(R.styleable.AirConditionerView_density, 3);
                displayView.setDensity(mDensity);
                float maxAngle = typedArray.getInt(R.styleable.AirConditionerView_maxAngle, 300);
                displayView.setSwipeMaxAngle(maxAngle);
                long startAnimTime = typedArray.getInt(R.styleable.AirConditionerView_startAnimTime, 500);
                displayView.setStartAnimlTime(startAnimTime);
                long addOrReduiceAnimTime = typedArray.getInt(R.styleable.AirConditionerView_addOrReduiceAnimTime, 50);
                displayView.setAnimTime(addOrReduiceAnimTime);
                long scalePadding = typedArray.getInt(R.styleable.AirConditionerView_scalePadding, 10);
                displayView.setPadding(scalePadding);
                float scaleLength = typedArray.getDimension(R.styleable.AirConditionerView_scaleLength, 5);
                displayView.setScaleLength(scaleLength);
                float scaleWidth = typedArray.getDimension(R.styleable.AirConditionerView_scaleWidth, 2);
                displayView.setScaleWidth(scaleWidth);
                float scaleReduce = typedArray.getDimension(R.styleable.AirConditionerView_scaleReduce, 5);
                displayView.setReduce(scaleReduce);
                int scaleColor = typedArray.getColor(R.styleable.AirConditionerView_scaleColor, Color.BLUE);
                displayView.setSwipeScaleColor(scaleColor);
                int maxScaleColor = typedArray.getColor(R.styleable.AirConditionerView_maxScaleColor, Color.WHITE);
                displayView.setAllScaleColor(maxScaleColor);
                int startColor = typedArray.getColor(R.styleable.AirConditionerView_startColor, Color.WHITE);
                displayView.setStartColor(startColor);
                int endColor = typedArray.getColor(R.styleable.AirConditionerView_endColor, Color.WHITE);
                displayView.setEndColor(endColor);

            }
        }


        textDescribePaint = new Paint();
        textDescribePaint.setAntiAlias(true);
        textDescribePaint.setColor(textDescribeColor);
        textDescribePaint.setTextSize(textDescribeSize);

        textTemperaturePaint = new Paint();
        textTemperaturePaint.setAntiAlias(true);
        textTemperaturePaint.setColor(textTemperatureColor);
        textTemperaturePaint.setTextSize(textTemperatureSize);

        textUnitPaint = new Paint();
        textUnitPaint.setAntiAlias(true);
        textUnitPaint.setColor(textUnitColor);
        textUnitPaint.setTextSize(textUnitSize);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (width == 0) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        if (height == 0) {
            height = MeasureSpec.getSize(heightMeasureSpec);
        }
        minLen = Math.min(width, height);

        x = y = minLen / 2;
//        重新设置宽高使其为正方形
        setMeasuredDimension((int) minLen, (int) minLen);
    }

    private boolean isInt = true;

    @Override
    protected void onDraw(Canvas canvas) {

        if (isInt) {
            try {
                float value = Float.valueOf(temperatureStr);
                int data = (int) value;
                temperatureStr = data + "";
//                LogUtil.v(" temperatureStr = " + temperatureStr);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        float describeV = describe.length() * textDescribePaint.getTextSize();
        float temperaturePaintTextSize = textTemperaturePaint.getTextSize();
        float len = temperatureStr.length() * temperaturePaintTextSize;
        textUnitPaint.setColor(textUnitColor);
        float textUnitSize = textUnitPaint.getTextSize();
        //绘制默认的
        if (textUnitPanddinBottom == 0 && textUnitPanddinTop == 0) {
            canvas.drawText("℃", x * 3 / 2.f - textUnitSize / 2, y - y / 4.f + textUnitSize / 2, textUnitPaint);
        } else if (textUnitPanddinTop != 0) {
            canvas.drawText("℃", x * 3 / 2.f - textUnitSize / 2, textUnitPanddinTop, textUnitPaint);
        } else if (textUnitPanddinBottom != 0) {
            canvas.drawText("℃", x * 3 / 2.f - textUnitSize / 2, len - textUnitPanddinBottom - textUnitPaint.getTextSize(), textUnitPaint);
        }
        textDescribePaint.setColor(textDescribeColor);
        //绘制默认的
        if (describePanddinTop == 0 && describePanddinBottom == 0) {
            canvas.drawText(describe, x - describeV / 2, minLen - textDescribePaint.getTextSize() - 40, textDescribePaint);
        }
        if (describePanddinTop != 0)
            canvas.drawText(describe, x - describeV / 2, describePanddinTop, textDescribePaint);
        else if (describePanddinBottom != 0)
            canvas.drawText(describe, x - describeV / 2, minLen - textDescribePaint.getTextSize() - describePanddinBottom, textDescribePaint);

        Log.d("temperatureStr ", " == " + temperatureStr);

        textTemperaturePaint.setStyle(Paint.Style.FILL);
        textTemperaturePaint.setTextAlign(Paint.Align.CENTER);
        textTemperaturePaint.setColor(textTemperatureColor);
        Paint.FontMetrics fontMetrics = textTemperaturePaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = getHeight() / 2 + distance;
        canvas.drawText(temperatureStr, getWidth() / 2, baseline, textTemperaturePaint);

    }


    public void setTemperature(String temperature) {
        try {
            float v = Float.valueOf(temperature);
            setTemperature(v);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public float getMinTemperature() {
        return displayView.getMinTemp();
    }

    public float getMaxTemperature() {
        return displayView.getMaxTemp();
    }

    public int getTemperature() {
        return (int) temperatureInt;
    }

    @SuppressLint("LongLogTag")
    public void setTemperature(float temperature) {
        displayView.setTemp(temperature);
        temperatureInt = temperature;
        temperatureStr = temperatureInt + "";
        postInvalidate();
    }

    public void addTemp() {
        displayView.addTemp();
        temperatureInt = displayView.getNowTemp();
        temperatureStr = temperatureInt + "";
        postInvalidate();
    }

    public void reduceTemp() {
        displayView.reduceTemp();
        temperatureInt = displayView.getNowTemp();
        temperatureStr = temperatureInt + "";
        postInvalidate();
    }

    public void setMaxTemperature(float i) {
        displayView.setMaxTemp(i);
    }

    public void setMinTemperature(int i) {
        displayView.setMinTemp(i);
    }

    public void setTextTemperatureColor(int textTemperatureColor) {
        this.textTemperatureColor = textTemperatureColor;
        textUnitColor = textTemperatureColor;
    }

    public void setTextDescribeColor(int textDescribeColor) {
        this.textDescribeColor = textDescribeColor;
    }
}
